import { Injectable, UnauthorizedException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Clerk } from 'src/core/entities/clerk.entity';
import { Postman } from 'src/core/entities/postman.entity';
import { SysUser } from 'src/core/entities/sysuser.entity';
import { User } from 'src/core/entities/user.entity';
import { Repository } from 'typeorm';
import { JwtService } from '@nestjs/jwt';
import { AuthResDto, AuthTokenDto, AuthTypes } from './auth.dto';
import { WechatService } from 'src/kits/services/wechat.service';
import { DouyinService } from 'src/kits/services/douyin.service';
import { KuaishouService } from 'src/kits/services/kuaishou.service';

@Injectable()
export class AuthService {
  constructor(
    @InjectRepository(User)
    private readonly userRepository: Repository<User>,
    @InjectRepository(SysUser)
    private readonly sysUserRepository: Repository<SysUser>,
    @InjectRepository(Postman)
    private readonly postmanRepository: Repository<Postman>,
    @InjectRepository(Clerk)
    private readonly clerkRepository: Repository<Clerk>,
    private readonly jwtService: JwtService,
  ) {}

  async validateSysUser(phone: string, password: string): Promise<AuthResDto> {
    const sysUser = await this.sysUserRepository.findOne({
      where: {
        phone,
      },
      select: ['id', 'password'],
    });

    if (sysUser?.password !== password) {
      throw new UnauthorizedException('账号或者密码错误');
    }
    const token = this.jwtService.sign({
      id: sysUser.id,
      type: AuthTypes.SYSUSER,
    });

    return {
      token,
    };
  }

  async validateUser(phone: string, password: string): Promise<AuthResDto> {
    const user = await this.userRepository.findOne({
      where: {
        phone,
      },
      select: ['id', 'password'],
    });
    if (user?.password !== password) {
      throw new UnauthorizedException('账号或者密码错误');
    }
    const token = this.jwtService.sign({
      id: user.id,
      type: AuthTypes.USER,
    });

    return {
      token,
    };
  }

  async validatePostman(phone: string, password: string): Promise<AuthResDto> {
    const postman = await this.postmanRepository.findOne({
      where: {
        phone,
      },
      select: ['id', 'password'],
    });
    if (postman?.password !== password) {
      throw new UnauthorizedException('账号或者密码错误');
    }
    const token = this.jwtService.sign({
      id: postman.id,
      type: AuthTypes.POSTMAN,
    });

    return {
      token,
    };
  }

  async validateClerk(phone: string, password: string): Promise<AuthResDto> {
    const clerk = await this.clerkRepository.findOne({
      where: {
        phone,
      },
      select: ['id', 'password'],
    });
    if (clerk?.password !== password) {
      throw new UnauthorizedException('账号或者密码错误');
    }
    const token = await this.jwtService.signAsync({
      id: clerk.id,
      type: AuthTypes.CLERK,
    });
    return {
      token,
    };
  }

  async validateToken(type: AuthTypes, id: number): Promise<boolean> {
    switch (type) {
      case AuthTypes.SYSUSER:
        return this.sysUserRepository.existsBy({ id });
        break;
      case AuthTypes.USER:
        return this.userRepository.existsBy({ id });
        break;
      case AuthTypes.POSTMAN:
        return this.userRepository.existsBy({ id });
        break;
      case AuthTypes.CLERK:
        return this.userRepository.existsBy({ id });
        break;
    }
  }
}
